import actionTypes from './actionTypes'
import { getOverallGraph } from '../services'


const startFetchOverallGraph = () => {
    return {
        type: actionTypes.START_FETCH_OVERALL_GRAPH
    }
}

const fetchOverallGraphSuccess = (payload) => {
    return {
        type: actionTypes.FETCH_OVERALL_GRAPH_SUCCESS,
        payload
    }
}

const fetchOverallGraphFailed = () => {
    return {
        type: actionTypes.FETCH_OVERALL_GRAPH_FAILED
    }
}

export const fetchOverallGraph = () => dispatch => {
    dispatch(startFetchOverallGraph())
    getOverallGraph()
        .then(response => {
            console.log(response)
            if(response.status === 200) {
                dispatch(fetchOverallGraphSuccess({
                    graph: response.data
                }))
            } else {
                dispatch(fetchOverallGraphFailed())
            }
        })
        .catch(error => {
            console.log(error)
            dispatch(fetchOverallGraphFailed())
        })
}

export const changeOverallGraphByLegend = (payload) => {
    return {
        type: actionTypes.CHANGE_OVERALL_GRAPH_BY_LEGEND,
        payload
    }
}